<?php
    class block_categoryBlock_controller extends controllerBase
    {
        public function process()
        {
            $oDatabase = new database();
            // Get Parent Category
            $sQuery = " PREFIX foaf: <http://xmlns.com/foaf/0.1/>
                        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                        PREFIX dc: <http://purl.org/dc/elements/1.1/>
                        PREFIX bus: <".URL::getLink('id/bussiness#').">

                        SELECT * 
                        WHERE
                        {
                            ?uri rdf:type bus:category.
                            ?uri bus:name ?name .
                            OPTIONAL { ?uri bus:subCategory ?category}
                            FILTER( !bound( ?category ) )
                        } ";
            
            $aParentCategories = $oDatabase->loadQuery($sQuery);
            $this->aParams['aParentCategories'] = $aParentCategories['result']['rows'];
            // Get All category
            $sQuery = " PREFIX foaf: <http://xmlns.com/foaf/0.1/>
                        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                        PREFIX dc: <http://purl.org/dc/elements/1.1/>
                        PREFIX bus: <".URL::getLink('id/bussiness#').">

                        SELECT * 
                        WHERE
                        {
                           ?uri rdf:type bus:category.
                           ?uri bus:name ?name .
                           OPTIONAL 
                           {
                              ?uri bus:childCategoryOf ?pCategoryUri.
                              ?pCategoryUri bus:name ?pCategoryName.
                           }
                        } ";
            
            $aResults = $oDatabase->loadQuery($sQuery);
            $sCategoryData = '';
            $aCategories = array();
            // Xay dung mang catagory gom cac catagory thanh cung mot row
            foreach($aResults['result']['rows'] as $aCategory)
            {
                if(!isset($aCategories[$aCategory['uri']]))
                {
                    $aCategories[$aCategory['uri']] = array(
                            'uri' => $aCategory['uri'],
                            'name' => $aCategory['name'],
                            'sParentCategory' => '',
                            'parentCategory' => array(
                                    'pCategoryUri' => array(
                                            'pCategoryUri' => (isset($aCategory['pCategoryUri']) ? $aCategory['pCategoryUri'] : ''),
                                            'pCategoryName' => (isset($aCategory['pCategoryName']) ? $aCategory['pCategoryName'] : ''),
                                    )
                                ),
                        );
                    if(isset($aCategory['pCategoryUri']))
                    {
                        $aCategories[$aCategory['uri']]['sParentCategory'] .= $aCategory['pCategoryUri'] . ';';
                    }
                }
                else
                {
                    $aCategories[$aCategory['uri']]['parentCategory'][$aCategory['pCategoryUri']] = array(
                            'pCategoryUri' => (isset($aCategory['pCategoryUri']) ? $aCategory['pCategoryUri'] : ''),
                            'pCategoryName' => (isset($aCategory['pCategoryName']) ? $aCategory['pCategoryName'] : ''),
                        );
                    if(isset($aCategory['pCategoryUri']))
                    {
                        $aCategories[$aCategory['uri']]['sParentCategory'] .= $aCategory['pCategoryUri'] . ';';
                    }
                }
            }
            
            foreach($aCategories as $aCategory)
            {
                $sStr = $aCategory['name']."__".$aCategory['uri']."__".$aCategory['sParentCategory'];
                $sCategoryData = $sCategoryData.$sStr."|";
            }
            $sCategoryData = chop($sCategoryData,'|');
            $this->aParams['sCategoryData'] = $sCategoryData;
            $this->aParams['aCategories'] = $aCategories;
        }
    }
?>